﻿using System;
using System.Collections.Generic;
using System.Text;
using ORMapping;

namespace DAL
{
    public class UserDAL
    {
        private EntityControl<User> control;

        public UserDAL()
        {
            control = EntityControl<User>.CreateEntityControl("ORMapping");
        }

        public void AddUser(User user)
        {
            control.AddEntity(user);
        }

        public void UpdateUser(User user)
        {
            control.UpdateEntity(user, user.UserID);
        }

        public void DeleteUser(User user)
        {
            control.DeleteEntity(user);
        }

        public IList<User> GetAllUsers(string strHQL, System.Collections.Generic.List<HQLParameter> hp)
        {
            return control.GetEntities(strHQL, hp);
        }

        public User Login(string strLoginName, String strPasswd)
        {
            string strHQL = "from User u where u.LoginName = :LoginName and u.Passwd = :Passwd";
            System.Collections.Generic.List<HQLParameter> hp = new System.Collections.Generic.List<HQLParameter>(2);
            hp.Add(new HQLParameter(EnumHQLParameterType.STRING, "LoginName", strLoginName.ToLower()));
            hp.Add(new HQLParameter(EnumHQLParameterType.STRING, "Passwd", strPasswd));
            IList<User> lUser = control.GetEntities(strHQL, hp);
            return lUser.Count > 0 ? lUser[0] : null;
        }
    }
}
